package com.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bean.Tunnel;
import com.service.TunnelServiceimpl;
import com.service.TunnelService;

/**
 * Servlet implementation class TunnelServiet
 */
@WebServlet("/TunnelServiet")
public class TunnelServiet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public TunnelServiet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String action = request.getParameter("action"); // 判断用户的请求类型，比如新增，查询或者修改等
		TunnelService service = new TunnelServiceimpl(); // 需要用到业务逻辑层的对象
		if(action==null||"".equals(action)||"list".equals(action)){
			String suidao = request.getParameter("suidaos");
			Tunnel att = new Tunnel();
			att.setSuidao(suidao);
			List<Tunnel> list =  service.getList(att);
			request.setAttribute("myList", list); // 设置数据到request里面
			request.setAttribute("att", att);
			request.getRequestDispatcher("list.jsp").forward(request, response);
		}
		if("add".equals(action)){
			//1.接受上个页面的数据
			String suidao =request.getParameter("suidao");
			String zhuangtao =request.getParameter("zhuangtao");
			String changdu =request.getParameter("changdu");
			String kuan=request.getParameter("kuan");
			String gao=request.getParameter("gao");
			String xiujianniandu=request.getParameter("xiujianniandu");
			String gaijianniandu=request.getParameter("gaijianniandu");
			String gaijianname=request.getParameter("gaijianname");
			String shejiname=request.getParameter("shejiname");
			String shigongname=request.getParameter("shigongname");
			String jianliname=request.getParameter("jianliname");
			String tongcheshijian=request.getParameter("tongcheshijian");
			String guanyangname=request.getParameter("guanyangname");
			String jishudengji=request.getParameter("jishudengji");
			String pingdingriqi=request.getParameter("pingdingriqi");
			String pingdingdanwei=request.getParameter("pingdingdanwei");
			String zhengqudaima=request.getParameter("zhengqudaima");
			String beizhu=request.getParameter("beizhu");
			Tunnel info=new Tunnel();
			info.setSuidao(suidao);
			info.setZhuangtao(Integer.parseInt(zhuangtao));
			info.setChangdu(Integer.parseInt(changdu));
			info.setKuan(Integer.parseInt(kuan));
			info.setGao(Integer.parseInt(gao));
			info.setXiujianniandu(xiujianniandu);
			info.setGaijianniandu(gaijianniandu);
			info.setGaijianname(gaijianname);
			info.setShejiname(shejiname);
			info.setShigongname(shigongname);
			info.setJianliname(jianliname);
			info.setTongcheshijian(tongcheshijian);
			info.setGuanyangname(guanyangname);
			info.setJishudengji(jishudengji);
			info.setPingdingriqi(pingdingriqi);
			info.setPingdingdanwei(pingdingdanwei);
			info.setZhengqudaima(zhengqudaima);
			info.setBeizhu(beizhu);
	
			//2.调用业务逻辑层的方法
			service.add(info);
			//3.准备数据，并进行页面跳转
			response.sendRedirect("TunnelServiet");
		}
		if("del".equals(action)){
			String luduan=request.getParameter("luduan");
			service.del(Integer.parseInt(luduan));
			response.sendRedirect("TunnelServiet");
		}
		if("preUpdate".equals(action)){
			String idStr=request.getParameter("luduan");
			Tunnel info=new Tunnel();
			int luduan=Integer.parseInt(idStr);
			info.setLuduan(luduan);
			service.getList(info);
			List<Tunnel> list=service.getList(info);
			if(list!=null&&list.size()>0){
				request.setAttribute("info", list.get(0));
				request.getRequestDispatcher("update.jsp").forward(request, response);
		    
			}
		}
		if("update".equals(action)) {
			
			String suidao =request.getParameter("suidao ");
			String zhuangtao =request.getParameter("zhuangtao");
			String changdu =request.getParameter("changdu");
			String kuan=request.getParameter("kuan");
			String gao=request.getParameter("gao");
			String xiujianniandu=request.getParameter("xiujianniandu");
			String gaijianniandu=request.getParameter("gaijianniandu");
			String gaijianname=request.getParameter("gaijianname");
			String shejiname=request.getParameter("shejiname");
			String shigongname=request.getParameter("shigongname");
			String jianliname=request.getParameter("jianliname");
			String tongcheshijian=request.getParameter("tongcheshijian");
			String guanyangname=request.getParameter("guanyangname");
			String jishudengji=request.getParameter("jishudengji");
			String pingdingriqi=request.getParameter("pingdingriqi");
			String pingdingdanwei=request.getParameter("pingdingdanwei");
			String zhengqudaima=request.getParameter("zhengqudaima");
			String beizhu=request.getParameter("beizhu");
			String idStr = request.getParameter("luduan");
			int luduan = Integer.parseInt(idStr);
			Tunnel info=new Tunnel();
			info.setSuidao(suidao);
			info.setZhuangtao(Integer.parseInt(zhuangtao));
			info.setChangdu(Integer.parseInt(changdu));
			info.setKuan(Integer.parseInt(kuan));
			info.setGao(Integer.parseInt(gao));
			info.setXiujianniandu(xiujianniandu);
			info.setGaijianniandu(gaijianniandu);
			info.setGaijianname(gaijianname);
			info.setShejiname(shejiname);
			info.setShigongname(shigongname);
			info.setJianliname(jianliname);
			info.setTongcheshijian(tongcheshijian);
			info.setGuanyangname(guanyangname);
			info.setJishudengji(jishudengji);
			info.setPingdingriqi(pingdingriqi);
			info.setPingdingdanwei(pingdingdanwei);
			info.setZhengqudaima(zhengqudaima);
			info.setBeizhu(beizhu);
			info.setLuduan(luduan);
			service.update(info);
			response.sendRedirect("TunnelServiet");
		}
	}


	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
